/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.em.service.impl;

import me.zhengjie.modules.cspimp.em.domain.TeamMember;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.em.repository.TeamMemberRepository;
import me.zhengjie.modules.cspimp.em.service.TeamMemberService;
import me.zhengjie.modules.cspimp.em.service.dto.TeamMemberDto;
import me.zhengjie.modules.cspimp.em.service.dto.TeamMemberQueryCriteria;
import me.zhengjie.modules.cspimp.em.service.mapstruct.TeamMemberMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author zmm
* @date 2020-05-24
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TeamMemberServiceImpl implements TeamMemberService {

    private final TeamMemberRepository teamMemberRepository;
    private final TeamMemberMapper teamMemberMapper;

    @Override
    public Map<String,Object> queryAll(TeamMemberQueryCriteria criteria, Pageable pageable){
        Page<TeamMember> page = teamMemberRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(teamMemberMapper::toDto));
    }

    @Override
    public List<TeamMemberDto> queryAll(TeamMemberQueryCriteria criteria){
        return teamMemberMapper.toDto(teamMemberRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public TeamMemberDto findById(Long id) {
        TeamMember teamMember = teamMemberRepository.findById(id).orElseGet(TeamMember::new);
        ValidationUtil.isNull(teamMember.getId(),"TeamMember","id",id);
        return teamMemberMapper.toDto(teamMember);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TeamMemberDto create(TeamMember resources) {
        return teamMemberMapper.toDto(teamMemberRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TeamMember resources) {
        TeamMember teamMember = teamMemberRepository.findById(resources.getId()).orElseGet(TeamMember::new);
        ValidationUtil.isNull( teamMember.getId(),"TeamMember","id",resources.getId());
        teamMember.copy(resources);
        teamMemberRepository.save(teamMember);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            teamMemberRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TeamMemberDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TeamMemberDto teamMember : all) {
            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("公司id", teamMember.getCid());
//            map.put("队员bid", teamMember.getUserId());
//            map.put("队伍bid", teamMember.getTeamId());
//            map.put("创建时间", teamMember.getCreateTime());
//            map.put("更新时间", teamMember.getUpdateTime());
//            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}